﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using PagedList.Mvc;

namespace shop.Areas.Main.Controllers
{
    public class PartialController : Controller
    {
        private LongHAEntities db = new LongHAEntities();
        [ChildActionOnly]
        public PartialViewResult GetDanhMuc()
        {
           // LongHAEntities db = new LongHAEntities();
            ViewBag.data = db.DanhMucs.ToList();
            return PartialView();
        }
        [ChildActionOnly]
        public PartialViewResult GetThuongHieu()
        {
//LongHAEntities db = new LongHAEntities();
            ViewBag.Data = db.ThuongHieux.ToList();
            return PartialView();
        }
        [ChildActionOnly]
        public PartialViewResult GetNgheNghiep()
        {
           // LongHAEntities db = new LongHAEntities();
            ViewBag.Data = db.NgheNghieps.ToList();
            return PartialView();
        }
        public PartialViewResult MenuNgheNghiep(string vitri)
        {
            var data = db.NgheNghieps.Where(s=>s.ViTri==vitri).ToList();
            return PartialView(data);
        }
        [ChildActionOnly]
        public PartialViewResult Getslide()
        {
           // LongHAEntities db = new LongHAEntities();
            ViewBag.data = db.Slides.ToList();
            return PartialView();
        }
        [ChildActionOnly]
        public PartialViewResult GetGioiThieu()
        {
           // LongHAEntities db = new LongHAEntities();
            ViewBag.data = db.GioiThieux.ToList();
            return PartialView();
        }
        [ChildActionOnly]
        public PartialViewResult GetSanPhamMoiNhat()
        {
           // LongHAEntities db = new LongHAEntities();
            ViewBag.data = db.SanPhams.OrderByDescending(s=>s.ID).Take(4).ToList();
            return PartialView();
        }
            [ChildActionOnly]
        public PartialViewResult GetTenDanhMuc(string MaDM )
        {
           // LongHAEntities db = new LongHAEntities();
            var data = db.DanhMucs.Where(s=>s.MaDM==MaDM).ToList();                         
                ViewBag.data = data;
                ViewBag.tendanhmuc = data.First().DanhMuc1;
                ViewBag.iddanhmuc = data.First().MaDM;
                return PartialView();       
        }

            public PartialViewResult GetTemThuongHieu(string MaTH)
            {
                var data = db.ThuongHieux.Where(s => s.MaTH == MaTH).ToList();
                ViewBag.tenthuonghieu = data.FirstOrDefault().ThuongHieu1;
                ViewBag.idthuonghieu = data.FirstOrDefault().MaTH;
                return PartialView();
            }
            [ChildActionOnly]
        public PartialViewResult GetMaMau(string MaMau)
        {
           // LongHAEntities db = new LongHAEntities();
            var data = db.Maus.Where(s => s.MaMau == MaMau).ToList();
            return PartialView(data);
        }
            [ChildActionOnly]
        public PartialViewResult GetSanPhamMau(string MaSP)
        {
            LongHAEntities db = new LongHAEntities();
            var data = db.SanPham_Mau.Where(s => s.MaSP == MaSP).ToList();
            return PartialView(data);
        }
        [ChildActionOnly]
        public PartialViewResult GetKichThuoc(string MaSP)
        {
           // LongHAEntities db = new LongHAEntities();
            var data = db.SanPham_KichThuoc.Where(s=>s.MaSP==MaSP).ToList();
            return PartialView(data);
        }
       
        public PartialViewResult SanPhamNgauNhien()
        {
            LongHAEntities db = new LongHAEntities();
            var data = 
                (from s in db.SanPhams
                    orderby Guid.NewGuid()
                    select s
                       ).Take(4);
            return PartialView(data);
        }
        [ChildActionOnly]
        public PartialViewResult SanPhamByDM(string MaDM)
        {
            var data = db.SanPhams.Where(s => s.MaDM == MaDM).ToList();
            return PartialView(data);
        }
        public PartialViewResult SanPhamByTH(string MaTH)
        {
            var data = db.SanPhams.Where(s=>s.MaTH==MaTH).ToList();
            if(data==null)
            {
                return PartialView("404.cshtml");
            }
            return PartialView(data);
        }
        public PartialViewResult SanPhamByNN(string MaNN)
        {
            var data = db.SanPhams.Where(s=>s.MaNN==MaNN).ToList();
            if(data == null )
            {
                return PartialView("404.cshtml");
            }
            return PartialView(data);
        }
        // drop
        public PartialViewResult dropmenu(string type)
        {
            if(type == "DM")
            {
                var data = db.DanhMucs.ToList(); ;
                ViewBag.type = type;
                ViewBag.data = data;
                return PartialView();
            }
            else
            {
                if(type=="TH")
                {
                    var data = db.DanhMucs.ToList();
                    ViewBag.type = type;
                    return PartialView(data);

                }
                else
                {

                    var data = db.DanhMucs.ToList();
                    ViewBag.type = type;
                    return PartialView(data);
                }
            }
        }
        public PartialViewResult dropth()
        {
            var data = db.ThuongHieux.Where(s=>s.Cha==true).ToList();
            return PartialView(data);
        }
        public PartialViewResult dropthbycha(int idcha)
        {
            var data = db.ThuongHieux.Where(s=>s.IdCon==idcha).ToList();
            return PartialView(data);
        }
        public PartialViewResult dropthTH(string MaTH, string type)
        {
            ViewBag.MaSP = MaTH;
            ViewBag.type = type;
            var data = db.ThuongHieux.Where(s => s.Cha == true).ToList();
            return PartialView(data);
        }
        public PartialViewResult dropthbychaTH(int idcha, string MaTH,string type )
        {
            ViewBag.MaSP = MaTH;
            ViewBag.type = type;
            var data = db.ThuongHieux.Where(s => s.IdCon == idcha).ToList();
            return PartialView(data);
        }


        public PartialViewResult dropnn(string MaNN,string type)
        {
            ViewBag.type = type;
            ViewBag.MaNN = MaNN;
            var data = db.NgheNghieps.ToList();
            return PartialView(data);
        }
        public PartialViewResult dropdm(string type, string MaDM)
        {
            ViewBag.type = type;
            ViewBag.MaDM = MaDM;
            var data = db.DanhMucs.Where(s=>s.Cha==true).ToList();
            return PartialView(data);
        }
        public PartialViewResult dropbycha(int idcha,string MaDM , string type)
        {
            ViewBag.type = type;
            ViewBag.MaDM = MaDM;
            var data = db.DanhMucs.Where(s => s.IDCon == idcha).ToList();
            return PartialView(data);
        }
        public PartialViewResult macdinh(string type)
        {
            var data = db.MacDinhs.Where(s => s.MaValue == type).ToList().LastOrDefault();
            ViewBag.value = data.Value;
            return PartialView();
        }
        public PartialViewResult BaiViet(string type)
        {
            var data = db.BaiViets.Where(s=>s.Value == type).ToList().FirstOrDefault();
            return PartialView(data);
        }
        public PartialViewResult GetTatCaSanPham()
        {
            var data = db.SanPhams.ToList();
            return PartialView(data);
        }

        //Thương hiệu
        public PartialViewResult GetThuongHieuCha(string vitri)
        {
            var data = db.ThuongHieux.Where(s => s.Cha == true && s.Vitri==vitri).ToList();
            return PartialView(data);
        }
       public PartialViewResult ThuongHieuByIdCha(int idcha)
        {
            var data = db.ThuongHieux.Where(s => s.IdCon == idcha).ToList();
            return PartialView(data);
        }
        // danh muc
       public PartialViewResult GetDanhMucCha(string vitri)
       {
           var data = db.DanhMucs.Where(s => s.Cha == true && s.ViTri == vitri).ToList();
           return PartialView(data);
       }
       public PartialViewResult DanhMucByIdCha(int idcha)
       {
           var data = db.DanhMucs.Where(s => s.IDCon == idcha).ToList();
           return PartialView(data);
       }
       public PartialViewResult DanhMucCha()
       {
           var data = db.DanhMucs.Where(s => s.Cha == true).ToList();
           return PartialView(data);
       }
       public PartialViewResult ThuongHieuCha()
       {
           var data = db.ThuongHieux.Where(s => s.Cha == true).ToList();
           return PartialView(data);
       }

       // Admin 
       public PartialViewResult DanhSanPham(int pagesize, int ? page=1)
       {
           var data = db.SanPhams.ToList();
           int pagenumber = (page ?? 1);
           return PartialView(data.ToPagedList(pagenumber,pagesize));
       }

        // 

        public PartialViewResult GetNgheNghiepHome()
       {
           var data = db.NgheNghieps.ToList();
           return PartialView(data);
       }
       
      public PartialViewResult MenuLeft(int id)
        {
          // có id cha 
          // xác định được các danh muc con 
          // từ các danh mục con  lấy ra sản phẩm             
            var dm = db.DanhMucs.Where(s => s.IDCon == id).ToList();
            
          return PartialView(dm);
        }

       public PartialViewResult LienKet()
      {
          var data = db.MacDinhs.Where(s => s.MaValue == "skype").ToList();
          return PartialView(data);
      }
        public PartialViewResult skypefooter()
       {
           var data = db.MacDinhs.Where(s => s.MaValue == "skype").ToList();
           return PartialView(data);
       }
        public PartialViewResult SanPhamTheoDM(string MaDM)
      {
          var sp = db.SanPhams.Where(s => s.MaDM == MaDM).OrderByDescending(s=>s.ID).Take(4).ToList();         
          return PartialView(sp);
      }
    }
}
